package com.pengweiwei.androidzygote.mine;

import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;

import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.pengweiwei.androidzygote.BR;
import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.mine.data.CollectionBean;
import com.pengweiwei.androidzygote.mine.data.MineApi;
import com.putong35.common.app.ui.AppListFragment;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.adapter.DataBindRecyclerViewAdapter;
import com.putong35.common.baseactfrag.adapter.DataBindViewHolder;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.httprequest.IHttpRequest;
import com.putong35.common.httprequest.UrlParams;
import com.putong35.common.httprequest.app.MyHttpRequest;
import com.putong35.common.httprequest.responsecallbacks.ListBeanResponseListener;
import com.putong35.common.httprequest.responsecallbacks.ListBeanResponseListenerImpl;
import com.putong35.common.httprequest.responsecallbacks.StringResponseListener;
import com.putong35.common.utils.EasyToast;

import java.util.List;


/**
 */
public class CollectionListFragment extends AppListFragment {
    public static final String TAG = CollectionListFragment.class.getSimpleName();

    private boolean isEditState = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestInfo(Request.Method.GET, MineApi.URL_COLLECT_LIST);
    }


    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new DataBindRecyclerViewAdapter<CollectionBean>(getActivity(), R.layout.listitem_collection, BR.collectionBean) {
            @Override
            public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
                DataBindViewHolder holder = (DataBindViewHolder) viewHolder;
                holder.getBinding().setVariable(BR.isEditState, isEditState);
                CheckBox checkBox = (holder.getBinding().getRoot().findViewById(R.id.rbtnCheck));
                checkBox.setOnCheckedChangeListener((compoundButton, checked) -> {
                    ((CollectionBean) CollectionListFragment.this.getData().get(i)).setIsChecked(checked);
                });


                super.onBindHolder(viewHolder, i);
            }
        };
    }


    @Override
    public ListBeanResponseListener makeResponseListener() {
        return new ListBeanResponseListenerImpl<CollectionBean>(CollectionBean.class, this);
    }


    @Override
    public void onListDataUpdateFailed(int code, String msg) {
        super.onListDataUpdateFailed(code, msg);
        EasyToast.showToast(getActivity(), msg);
    }

    public void updateState(boolean state) {
        this.isEditState = state;
        getAdapter().notifyDataSetChanged();
    }

    public void checkAll(boolean checkAll) {
        List data = getData();
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                CollectionBean bean = ((CollectionBean) data.get(i));
                bean.setIsChecked(checkAll);
//                ((CollectionBean) data.get(i)).setIsChecked(checkAll);
            }
            getAdapter().notifyDataSetChanged();

        }
    }

    public void clkDelete() {
        StringBuilder sbIds = new StringBuilder();
        List data = getData();
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                CollectionBean bean = ((CollectionBean) data.get(i));
                Log.d(TAG, "clkDelete: " + bean);
                if (bean.getIsChecked()) {
                    sbIds.append(bean.getCollectId()).append(",");
                }
            }
        }
        String ids = sbIds.toString();
        if (ids.endsWith(",")) {
            ids = sbIds.substring(0, ids.length() - 1);
        }

        setState(CompState.EMPTY_REFRESHING);
        UrlParams urlParams = new UrlParams();
        urlParams.put("ids", ids);
        IHttpRequest request = new MyHttpRequest(getActivity());
        request.get(MineApi.URL_COLLECT_DEL, urlParams, new StringResponseListener() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                setState(CompState.DATA);
                EasyToast.showToast(getActivity(), strMsg);
            }

            @Override
            public void onSuccess(String bean) {
                setState(CompState.DATA);
                EasyToast.showToast(getActivity(), "成功取消收藏");
                reload();
            }
        });



       /* HashMap<String, String> map = new HashMap<>();
        map.put("ids", sbIds.toString());
        HttpCommonApiHelper.httpGet(MineApi.URL_COLLECT_DEL, map, String.class, new OnSuccessListener<String>() {
            @Override
            public void onSuccess(int code, String msg, String data, Object extObj) {
                Log.d(TAG, "onSuccess: " + data);
            }
        }, new OnFailureListener() {
            @Override
            public void onFailure(int errCode, String msg) {
                Log.d(TAG, "onFailure: " + msg);
            }
        });*/
    }
}
